package it.mf.podcastgenius;

import it.mf.eclipseutils.PluginTool;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	protected static final String BUNDLE_IT_MF_PODCASTGENIUS = "it.mf.podcastgenius";

	// The plug-in ID
	public static final String PLUGIN_ID = BUNDLE_IT_MF_PODCASTGENIUS;

	// The shared instance
	private static Activator plugin;
	
	protected Connection dbConnection;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		Bundle bundle;
		String location;
		
		super.start(context);
		plugin = this;
		
		
		bundle=PluginTool.getPlugin(BUNDLE_IT_MF_PODCASTGENIUS);
		System.out.println(bundle.getLocation());
		
		File f=new File(bundle.getLocation());
		System.out.println(f.getAbsolutePath());
		//starting DB
		dbConnection = DriverManager.getConnection("jdbc:hsqldb:file:testdb", "sa", "");
		
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		Statement stm;
		plugin = null;
		super.stop(context);
		
		if(dbConnection!=null)
		{
			stm=dbConnection.createStatement();
			stm.execute("SHUTDOWN");
		}
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

}
